﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class RangeValidatorAttribute : BaseValidatorAttribute
    {
        public RangeValidatorAttribute(double minNumber, double maxNumber) : this(minNumber, maxNumber, ValidationUtils.Messages.Rangelength)
        {
        }

        public RangeValidatorAttribute(double minNumber, double maxNumber, string errorMessage) : base(errorMessage)
        {
            MaxNumber = maxNumber;
            MinNumber = minNumber;
        }

        public double MaxNumber { get; protected set; }
        public double MinNumber { get; protected set; }

        public override bool IsValid(object value)
        {
            var val = ValidationUtils.GetValueSize(value);
            if (val == null) return false;
            return val <= MaxNumber && val >= MinNumber;
        }

        public override string FormatErrorMessage()
        {
            return string.Format(ErrorMessage, MinNumber, MaxNumber);
        }
    }
}
